package com.mengniu.commerce.admin.controller.system;

import com.mengniu.commerce.admin.api.domain.SysStore;
import com.mengniu.commerce.admin.service.system.ISysStoreService;
import com.mengniu.commerce.common.core.domain.R;
import com.mengniu.commerce.common.core.utils.StringUtils;
import com.mengniu.commerce.common.core.web.controller.BaseController;
import com.mengniu.commerce.common.core.web.domain.AjaxResult;
import com.mengniu.commerce.common.core.web.page.TableDataInfo;
import com.mengniu.commerce.common.log.annotation.Log;
import com.mengniu.commerce.common.log.enums.BusinessType;
import com.mengniu.commerce.common.security.annotation.InnerAuth;
import com.mengniu.commerce.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 店铺信息Controller
 *
 * @author FanwuZhou
 * @date 2023-11-17
 */
@Controller
@RequestMapping("/store")
public class SysStoreController extends BaseController
{


    @Autowired
    private ISysStoreService sysStoreService;

    /**
     * 查询店铺信息列表
     */
    @RequiresPermissions("system:store:list")
    @PostMapping("/list")
    public TableDataInfo list(@Validated @RequestBody SysStore sysStore)
    {
        startPage();
        List<SysStore> list = sysStoreService.selectSysStoreList(sysStore);
        return getDataTable(list);
    }

    /**
     * 新增保存店铺信息
     */
    //@RequiresPermissions("system:store:add")
    @Log(title = "店铺信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addSave(@Validated @RequestBody SysStore sysStore)
    {
        return toAjax(sysStoreService.insertSysStore(sysStore));
    }

    /**
     * 修改保存店铺信息
     */
    @RequiresPermissions("system:store:edit")
    @Log(title = "店铺信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult editSave(@Validated @RequestBody SysStore sysStore)
    {
        return toAjax(sysStoreService.updateSysStore(sysStore));
    }

    /**
     * 删除店铺信息
     */
    @RequiresPermissions("system:store:remove")
    @Log(title = "店铺信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    public AjaxResult remove(String ids)
    {
        return toAjax(sysStoreService.deleteSysStoreByIds(ids));
    }



    /**
     * 通过storeId获取配置,用于生成token
     */
    @InnerAuth
    @GetMapping("/info/{storeCode}")
    public R<SysStore> info(@PathVariable("storeCode") String storeCode)
    {
        SysStore sysStore = sysStoreService.selectStoreByCode(storeCode);
        if (StringUtils.isNull(sysStore))
        {
            return R.fail("店铺不存在");
        }
        return R.ok(sysStore);
    }
}
